﻿#Requires AutoHotkey v2.0
#SingleInstance Force
#Include "const.ahk"
#Include "winapi_wrapper.ahk"

MinimizeWindow(&hMinimizedWindow) 
{
    hMinimizedWindow := GetForegroundWindow()
    if (!hMinimizedWindow)
        return
    ; minimize if legal
    ShowWindow(hMinimizedWindow, SW_MINIMIZE)
}

RestoreWindow(&hMinimizedWindow ) 
{
    if (!hMinimizedWindow) {
        return
    }
    ShowWindow(hMinimizedWindow, SW_RESTORE)
}

IMChangeRequest(*) 
{
    hForegroundWindow := GetForegroundWindow() ; local var
    if (!hForegroundWindow)
        return
    SendMessage(hForegroundWindow, WM_INPUTLANGCHANGEREQUEST, NULL, NULL)
}

ToggleTray(*) 
{
    hTrayWnd := FindWindow("Shell_TrayWnd", "")

    if (!IsWindowVisible(hTrayWnd)) {
        ShowWindow(hTrayWnd, SW_SHOWDEFAULT)
    }
    
    else {
        ShowWindow(hTrayWnd, SW_HIDE)
    }
}

IsWindowTopmost(hWnd) 
{
    xWindowInfo := GetWindowLong(hWnd, GWL_EXSTYLE)
    if (!(xWindowInfo & WS_EX_TOPMOST))
        return false
    return true
}

ToggleTopmostForegroundWindow(*)
{
    hForegroundWindow := GetForegroundWindow() ; local hForegroundWindow
    if (hForegroundWindow == 0) 
        return
    
    if (!IsWindowTopmost(hForegroundWindow)) { ; if not topmost, topmost it
        SetWindowPos(hForegroundWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE)
        SoundPlay(A_WinDir "\Media\Speech On.wav")
    } else { ; else untopmost it
        SetWindowPos(hForegroundWindow, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE)
        SoundPlay(A_WinDir "\Media\Speech Off.wav")
    }
}

GetMonitorInfo(lpRect)
{
    left := top := right := bottom := 0
    MonitorGet(MonitorGetPrimary(), &left, &top, &right, &bottom)
    NumPut("Int", left, %lpRect%, 0)
    NumPut("Int", top, %lpRect%, 4)
    NumPut("Int", right, %lpRect%, 8)
    NumPut("Int", bottom, %lpRect%, 12)
}

ConfineMouseCursorProc(lpRect)
{
    while (true)
    {
        ClipCursor(lpRect)
        Sleep(20)
    }
}

ConfineMouseCursor(lpRect)
{
    return CreateThread(
        NULL,
        0,
        CallbackCreate(ConfineMouseCursorProc, "C"),
        %lpRect%.Ptr,
        CREATE_SUSPENDED, ; do not run immediately
        NULL
    ) ; a handle to the created thread will be returned
}